#include "pam_pg_options.h"

modopt_t mod_options(int argc, const char **argv) {

   int i;
   char *ptr,*option,*value;
   modopt_t modopt = (modopt_t)malloc(sizeof(struct modopt_s));

   modopt->port = PAM_PG_PORT;
   modopt->debug = PAM_PG_DEBUG;

   for(i=0;i<argc;i++) {

      ptr = strchr(argv[i], '=');
      if(ptr != NULL) {

         option = strndup(argv[i], ptr-argv[i]);
         value = strndup(ptr+1, strchr(argv[i],'\0')-ptr);

         if( strcmp(option, "host") == 0 ) {
            modopt->host = strdup(value);
         } else if( strcmp(option, "fileconf") == 0 ) {
            modopt->fileconf = strdup(value);
         } else if( strcmp(option, "db") == 0 ) {
            modopt->db = strdup(value);
         } else if( strcmp(option, "user") == 0 ) {
            modopt->user = strdup(value);
         } else if( strcmp(option, "passwd") == 0 ) {
            modopt->passwd = strdup(value);
         } else if( strcmp(option, "debug") == 0 ) {
            modopt->debug = atoi(value);
         } else if( strcmp(option, "port") == 0 ) {
            modopt->port = atoi(value);
         }

      } else {

         if( strcmp(argv[i], "fileconf") == 0 ) {
            modopt->fileconf = strdup(PAM_PG_FILECONF);
         }

      }

   }

   if(modopt->host == NULL) modopt->host = strdup(PAM_PG_HOST);
   if(modopt->db == NULL) modopt->db = strdup(PAM_PG_DB);
   if(modopt->user == NULL) modopt->user = strdup(PAM_PG_USER);
   if(modopt->passwd == NULL) modopt->passwd = strdup(PAM_PG_PASSWD);

   return modopt;

}
